﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Visifire.Charts;
using Visifire.Commons;

using OneStop.Calculations;


namespace OneStop.UI
{
    public class MainProvider : INotifyPropertyChanged
    {
        public readonly string PrefsFilename = "Prefs";

        public MainProvider()
        {

        }

        public void Refresh()
        {
            NotifyPropertyChanged("ProfilePrimary");
            NotifyPropertyChanged("LineSeriesPrimary");
            NotifyPropertyChanged("PrimaryDataPoints");
            NotifyPropertyChanged("Prefs");
            NotifyPropertyChanged("IsInstalled");
            NotifyPropertyChanged("IsFullScreen");
            NotifyPropertyChanged("IsCCRDive");
            NotifyPropertyChanged("SavedProfiles");
            NotifyPropertyChanged("OutputSegments");
            NotifyPropertyChanged("IsInstallable");
            NotifyPropertyChanged("IsFullScreen");
            NotifyPropertyChanged("BailDiff");            
        }

        #region Interface Methods

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion

        public bool IsMetric
        {
            get
            {
                return (Prefs.Units == Units.Metric);
            }

            set
            {
                Prefs.Units = (value) ? Units.Metric : Units.Imperial;
            }
        }

        public double? BailDiff
        {
            get
            {
                double? diff = null;

                if (this.IsCCRDive && this.ProfileBailout != null)
                {
                    diff = this.ProfilePrimary.RunTime - this.ProfileBailout.RunTime;
                }

                return diff;
            }
        }

        public Profile ProfilePrimary { get; set; }
        public Profile ProfileBailout { get; set; }

        private ObservableCollection<SegmentAbstract> _OutputSegments;

        public ObservableCollection<SegmentAbstract> OutputSegments //{ get; set; }
        {
            get
            {
                return _OutputSegments;
            }
            set
            {
                _OutputSegments = value;
                NotifyPropertyChanged("OutputSegments");
            }               
        }

        public string[] SavedProfiles
        {
            get
            {
                using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    return isf.GetFileNames().Where(s => s != PrefsFilename).ToArray<string>();
                }  
            }
        }

        public bool IsCCRDive
        {
            get
            {
                if (ProfilePrimary != null)
                {
                    foreach (SegmentAbstract s in ProfilePrimary.InputSegments)
                    {
                        if (s.Setpoint > 0) return true;
                    }
                }

                return false;
            }
        }

        public bool IsFullScreen
        {
            get
            {
                return Application.Current.Host.Content.IsFullScreen;
            }

            set
            {
                Application.Current.Host.Content.IsFullScreen = value;
            }
        }

        public bool IsInstallable
        {
            get
            {
                return !(Application.Current.InstallState == InstallState.Installed ||
                        Application.Current.InstallState == InstallState.Installing);
            }
        }

        public Prefs Prefs
        {
            get
            {
                return Prefs.Current;
            }
        }
    }
}
